package com.goro;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.context.FacesContext;

import com.joro.Magazine;
import com.joro.Supplier;
import com.joro.ejb.MagazineFacadeLocal;
import com.joro.ejb.SupplierFacadeLocal;

public class SupplierEditBean {

	@EJB
	protected SupplierFacadeLocal supplierDao;

	@EJB
	protected MagazineFacadeLocal magazineDao;
	private Supplier supplier;
	private int supplierId;
	private String action;
	private List<String> magazineItems = new ArrayList<String>();

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;

		if (this.action != null && this.action.equals("edit")) {
			this.getSupplierData();
		}

		if (this.action != null && this.action.equals("delete")) {
			this.deleteSupplier();		
			FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation(FacesContext.getCurrentInstance(), null, "success");
		}
	}

	public SupplierEditBean() {
		supplier = new Supplier();
	}

	public String getSupplierData() {
		if (this.supplierId > 0) {
			// Init the supplier member and the magazineItems of the current
			// supplier
			Supplier tempSupplier = supplierDao.getSupplierById(supplierId);
			if (tempSupplier != null) {
				this.supplier = tempSupplier;
				List<Magazine> magazines = this.supplier.getMagazines();
				this.magazineItems = new ArrayList<String>();

				if (magazines != null) {
					for (Magazine magazine : magazines) {
						this.magazineItems
								.add(String.valueOf(magazine.getId()));
					}
				}
			}
		}
		return null;
	}

	public int getSupplierId() {
		return supplierId;
	}

	public void setSupplierId(int supplierId) {
		this.supplierId = supplierId;
		this.supplier.setId(supplierId);

	}

	public List<String> getMagazineItems() {
		return magazineItems;
	}

	public void setMagazineItems(List<String> magazineItems) {
		this.magazineItems = magazineItems;
	}

	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier magazine) {
		this.supplier = magazine;
	}

	public String saveSupplier() {
		List<Magazine> magazines = new ArrayList<Magazine>();
		for (String idString : magazineItems) {
			int id = Integer.parseInt(idString);
			magazines.add(magazineDao.getMagazineById(id));
		}
		this.supplier.setMagazines(magazines);
		supplierDao.saveSupplier(this.supplier);
//		try {
//			FacesContext.getCurrentInstance().getExternalContext()
//					.redirect("suppliers.jsp");
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		return "success";
	}

	public String deleteSupplier() {
		supplierDao.deleteSupplier(supplier);
//		try {
//			FacesContext.getCurrentInstance().getExternalContext()
//					.redirect("suppliers.jsp");
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		return "success";
	}

	public String cancel() {
//		try {
//			FacesContext.getCurrentInstance().getExternalContext()
//					.redirect("suppliers.jsp");
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		return "success";
	}
}
